<?php 
namespace app\admin\controller;

use think\Controller;
use think\Session;
if (!Session::has('DSL','admin')){
	die('非法登录');
}

class Announcement extends Controller
{
	public function index()
	{
		$keyword = trim(input('get.keyword'));
		if(isset($keyword) && $keyword != ''){
			$announcement_list = db('announcement')->where('announcement','like','%'.$keyword.'%')->field(['id','announcement','created','has_content'])->paginate(10);
		}else{
			$announcement_list = db('announcement')->field(['id','announcement','created','has_content'])->paginate(10);
		}
		$page = $announcement_list->render();
		$announcement_list = $announcement_list->toArray()['data'];
		foreach ($announcement_list as $ak => &$av){
			$av['created'] = date('Y-m-d',$av['created']);
			$av['has_content'] = $av['has_content'] == 0?'off':'on';
		}
		$this->assign('page',$page);
		$this->assign('announcement_list',$announcement_list);
		return $this->fetch('index');
	}
	
	public function add_announcement()
	{
		$result = array('err' => '1','content' => '');
		$announcement_name = trim(input('post.announcement_name'));
		if ($announcement_name == '' || strlen($announcement_name) > 40){
			$result['content'] = '公告名字数请控制在1~20之间';
			return $result;
		}
		$model = model('Announcement');
		$announcement_info = array();
		$announcement_info['announcement'] = $announcement_name;
		$announcement_id = $model->saveAnnouncement($announcement_info);
		if ($announcement_id == false){
			$result['content'] = '添加公告失败！';
			return $result;
		}
		$result['err'] = '0';
		$result['content'] = '添加公告成功！';
		$result['announcement_id'] = $announcement_id;
		return $result;
	}
	
	public function del_announcement(){
		$result = array('err' => '1','content' => '');
		$announcement_id = trim(input('post.id'));
		$del_announcement = db('announcement')->where('id',$announcement_id)->delete();
		if ($del_announcement == false){
			$result['content'] = '删除公告失败！';
			return $result;
		}
		$result['err'] = '0';
		$result['content'] = '删除公告成功！';
		return $result;
	}
	
	public function edit_announcement()
	{
		$result = array('err' => '1','content' => '');
		$announcement_info['id'] = trim(input('post.announcement_id'));
		$announcement_info['announcement'] = trim(input('post.announcement_name'));
		$rules = [
				'id' => 'require|number',
				'announcement' => 'require|max:40'
		];
		$message = [
				'id.require' => '参数缺失！',
				'id.number' => ' 参数错误！',
				'announcement.require' => '公告名不能为空!',
				'announcement.max' => '请将公告名控制在20个字以内！'
		];
		$check_announcement_info = $this->validate($announcement_info, $rules,$message);
		if ($check_announcement_info != 'true'){
			$result['content'] = $check_announcement_info;
			return $result;
		}
		$model = model('Announcement');
		$edit_announcement = $model->saveAnnouncement($announcement_info);
		if ($edit_announcement == false){
			$result['content'] = '更新公告失败！';
			return $result;
		}
		$result['err'] = '0';
		$result['content'] = '更新公告成功！';
		return $result;
	}
	
	public function update_has()
	{
		$result = array('err' => '1','content' => '');
		$announcement_id = trim(input('get.announcement_id'));
		$announcement_has = trim(input('get.has'));
		if ($announcement_id == '' || $announcement_has == ''){
			$result['content'] = '参数错误！';
			return $result;
		}
		$announcement_list = array();
		$announcement_list['has_content'] = $announcement_has;
		$update_has = db('announcement')->where('id',$announcement_id)->update($announcement_list);
		
		if ($update_has == false){
			$result['content'] = '更新是否有详情页失败！';
			return $result;
		}
		$result['err'] = '0';
		$result['content'] = '更新是否详情页成功！';
		return $result;
	}
	
	public function save_content_view()
	{
		$announcement_id = trim(input('get.announcement_id'));
		$announcement_info = db('announcement')->where('id',$announcement_id)->find();
		$this->assign('announcement',$announcement_info);
		return $this->fetch('content');
	}
	
	public function save_content()
	{
		$result = array('err' => '1','content' => '');
		$announcement = input('post.');
		$rules = [
				'id' => 'require|number',
				'announcement' => 'require|max:40',
				'descr' => 'require|max:100'
		];
		$massege = [
				'id.require' => '参数缺失！',
				'id.number' => '参数错误！',
				'announcement.require' => '公告名必须！',
				'announcement.max' => '公告名的字数请控制在20以内！',
				'descr.require' => '描述必须！',
				'descr.max' => '描述的字数请控制在50以内！'
		];
		$check_announcement = $this->validate($announcement, $rules,$massege);
		if ($check_announcement != 'true'){
			$result['content'] = $check_announcement;
			return $result;
		}
		$announcement_list['announcement'] = $announcement['announcement'];
		$announcement_list['descr'] = $announcement['descr'];
		$announcement_list['content'] = $announcement['content'];
		$save_content = db('announcement')->where('id',$announcement['id'])->update($announcement_list);
		if ($save_content == false){
			$result['content'] = '保存公告失败！';
			return $result;
		}
		$result['err'] = '0';
		$result['content'] = '保存公告成功！';
		return $result;
	}
}